/**
 * HTTP请求参数配置
 * 创建人：王秦
 * 创建日期： 2017-09-24
 */
// import { StorageService } from '../js/storage.service'
import wepy from 'wepy'
export class RequestService {
  static httpHandlerError(info, callBack, errTip) {
    wepy.hideLoading()
    if ((info.statusCode >= 200 && info.statusCode <= 207) || info.statusCode === 304) {
      return false
    } else {
      if (info.statusCode === 401) {
        wepy.redirectTo({
          url: 'index'
        })
      }
      let errorInfo = ''
      if (errTip) {
        errorInfo = errTip
      } else {
        if (info.data.message) {
          errorInfo = info.data.message
        } else {
          errorInfo = '也许服务器忙!'
        }
      }
      wepy.showToast({
        title: errorInfo,
        icon: 'loading',
        duration: 3000
      })
      if (callBack) {
        callBack()
      }
      return true
    }
  }
  static soeRequest(method, reqData, reqUrl, failFn, sucFn) {
    wepy.request({
      // header: this.getHeaders(),
      header: {
        'content-type': 'application/json'
      },
      data: reqData,
      url: reqUrl,
      method: method,
      complete: (res) => {
        let error = this.httpHandlerError(res, failFn)
        if (error) return
        sucFn(res)
      }
    })
  }
}
